import { View } from "../../../mvc/core/View";
import { SMail } from "../../../../Message/Game/SMail";
import { GameConst } from "../../../component/gconst/GameConst";
import { DragEvent } from "../../../../com/mui/events/DragEvent";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { MailEventName } from "./MailEventName";
import { DataEvent } from "../../events/DataEvent";
import { GLabel } from "../../../../com/mui/controls/GLabel";
import { HTMLUtil } from "../../../../com/gengine/utils/HTMLUtil";
import { Language } from "../../../../extend/language/Language";
import { ObjCreate } from "../common/ObjCreate";
import { GComboBox } from "../../../../com/mui/controls/GComboBox";
import { MailFriendItemCellRender } from "./MailFriendItemCellRender";
import { ResouceConst } from "../../../component/gconst/ResouceConst";
import { GTextFormat } from "../../../common/GTextFormat";
import { FontUtil } from "../../../common/font/FontUtil";
import { DataProvider } from "../../../../fl/data/DataProvider";
import { MTextArea } from "./MTextArea";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../resource/ImagesConst";
import { GButton } from "../../../../com/mui/controls/GButton";
import { AttachmentItem } from "./AttachmentItem";
import { Cache } from "../../cache/Cache";
import { FriendInfo } from "../../model/FriendInfo";
import { Alert } from "../../../../com/mui/controls/Alert";
import { StringHelper } from "../../../../com/gengine/utils/StringHelper";
import { MailUtil } from "./MailUtil";
import { InputMoneyPanel } from "./InputMoneyPanel";
import { BaseItem } from "../common/BaseItem";
import { InputAmountPanel } from "./InputAmountPanel";
import { EventName } from "../../mvc/EventName";
import { ItemData } from "../../resource/info/ItemData";
import { GTextInput } from "../../../../com/mui/controls/GTextInput";
import { _smail } from "../../../../this/_smail";
import { _uidDict } from "../../../../this/_uidDict";
type int = number;
//class SendPanel
    
    export  class SendPanel extends View
    {
        constructor()
        {
            super();
            this._smail = new SMail();
            this._itemsArray = [];
            this._postage = GameConst.SendMailCost;
            this._uidDict = new Map<any, any> /* flash.utils.Dictionary */();
            this.createChildren();
            this._sendBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onSendBtnClickHandler);
            this._inputMoneyBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onInputMoneyBtnClickHandler);
            this.addEventListener(DragEvent.Event_Move_In, this.moveInHandler);
            this.addEventListener(DragEvent.Event_Throw_goods, this.throwHandler);
            Dispatcher.addEventListener(MailEventName.INPUT_MONEY_OK, this.onInputMoneyOKClickHandler);
            Dispatcher.addEventListener(MailEventName.INPUT_AMOUNT_OK, this.onInputAmountOKClickHandler);
            return;
        }

        public onInputAmountOKClickHandler(arg1: DataEvent): void
        {
            this._amount = arg1.data["amount"];
            this._uidDict[arg1.data["uid"]] = this._amount;
            this._currentAcItem.amount = this._amount;
            return;
        }

        public /* override */ update(arg1: Object, ... rest): void
        {
            this._addresseeText.text = arg1["addressess"] as string;
            this._titleText.text = arg1["title"] as string;
            return;
        }

        public createChildren(): void
        {
            this._addresseeLabel = new GLabel();
            this._addresseeLabel.htmlText = HTMLUtil.addColor(Language.getString(52022), "#B1F0FF");
            this._addresseeLabel.setSize(47, 20);
            this._addresseeLabel.move(13, 7);
            this.addChild(this._addresseeLabel);
            this._addresseeText = ObjCreate.createGTextInput(62, 7, 120, 20, this);
            this._friendComboBox = new GComboBox();
            this._friendComboBox.editable = false;
            this._friendComboBox.styleName = "GComboboxStyle";
            this._friendComboBox.prompt = Language.getString(52023);
            this._friendComboBox.width = 100;
            this._friendComboBox.height = 20;
            this._friendComboBox.move(185, 7);
            this._friendComboBox.drawNow();
            this._friendComboBox.dropdown.setStyle("cellRenderer", MailFriendItemCellRender);
            this._friendComboBox.dropdown.setStyle("skin", ResouceConst.getScaleBitmap("WindowCenterB"));
            this._friendComboBox.textField.setStyle("textFormat", new GTextFormat(FontUtil.songtiName, 12, 16777215, null, null, null, null, null, null, null, null, 5, null));
            var loc1=new DataProvider();
            loc1.addItem({"label":"", "name": Language.getString(52023)});
            this._friendComboBox.dataProvider = loc1;
            this._friendComboBox.addEventListener(flash.events.Event.CHANGE, this.friendComboxChangeHandler);
            this._friendComboBox.addEventListener(flash.events.Event.OPEN, this.friendComboOpenHandler);
            this.addChild(this._friendComboBox);
            this._titleLabel = new GLabel();
            this._titleLabel.htmlText = HTMLUtil.addColor(Language.getString(52024), "#B1F0FF");
            this._titleLabel.move(13, 32);
            this.addChild(this._titleLabel);
            this._titleText = ObjCreate.createGTextInput(62, 30, 223, 20, this);
            this._titleText.name = "titleText";
            this._contentTextArea = new MTextArea();
            this._contentTextArea.editable = true;
            this._contentTextArea.width = 266;
            this._contentTextArea.height = 172;
            this._contentTextArea.move(0, 51);
            this.addChild(this._contentTextArea);
            this._goodsLabel = new GLabel();
            this._goodsLabel.htmlText = HTMLUtil.addColor(Language.getString(52002), "#04ED08");
            this._goodsLabel.setSize(20, 50);
            this._goodsLabel.move(13, 225);
            this.addChild(this._goodsLabel);
            this.createAttachItem();
            this._goldBitmap = GlobalClass.getBitmap("Yuanbao");
            this._goldBitmap.x = 171;
            this._goldBitmap.y = 235;
            this.addChild(this._goldBitmap);
            this._goldText = ObjCreate.createGTextInput(196, 233, 88, 22, this);
            this._goldText.editable = false;
            this._goldText.name = "goldText";
            this._goldText.text = "0";
            this._coinBitmap = GlobalClass.getBitmap("Tongqian");
            this._coinBitmap.x = 171;
            this._coinBitmap.y = 245;
            this._coinText = ObjCreate.createGTextInput(196, 245, 88, 20, null);
            this._coinText.editable = false;
            this._coinText.name = "coinText";
            this._coinText.text = "0";
            this._postageText = ObjCreate.createGTextInput(6, 268, 115, 20, this);
            this._postageText.htmlText = Language.getString(52025) + this._postage;
            this._postageText.editable = false;
            ObjCreate.createBitmap(ImagesConst.Tongqian, 80, 268, this);
            var loc2;
            this._sendBtn = loc2 = this.createGButton(123, 267, 65, 23, "InputMoneyButton", 52026);
            this._inputMoneyBtn = loc2;
            this._sendBtn = this.createGButton(192, 267, 45, 23, "SendButton", 52027);
            return;
        }

        /* internal  */createGButton(arg1: number, arg2: number, arg3: int, arg4: int, arg5: string, arg6: int): GButton
        {
            var loc1;
            (loc1 = ObjCreate.createGButton("", arg1, arg2, arg3, arg4, this, "MailBtn")).name = arg5;
            loc1.label = "";
            var loc2=(arg4 - 20) / 2 + 2;
            var loc3;
            (loc3 = ObjCreate.createGTextField("", 0, loc2, arg3, 20, loc1)).textColor = 11727614;
            var loc4;
            (loc4 = loc3.defaultTextFormat).align = flash.text.TextFormatAlign.CENTER;
            loc3.defaultTextFormat = loc4;
            loc3.htmlText = Language.getString(arg6);
            return loc1;
        }

        /* internal  */createAttachItem(): void
        {
            var loc2=null;
            var loc3=null;
            var loc1=0;
            while (loc1 < 3) 
            {
                loc2 = GlobalClass.getBitmap("PackItemBg");
                loc2.width = 38;
                loc2.height = 38;
                loc2.x = 34 + loc1 * 43;
                loc2.y = 226;
                this.addChild(loc2);
                loc3 = new AttachmentItem();
                loc3.setSize(32, 32);
                loc3.move(36 + loc1 * 44, 228);
                loc3.addEventListener(flash.events.MouseEvent.CLICK, this.onAttachmentClickHandler);
                this.addChild(loc3);
                this._itemsArray.push(loc3);
                ++loc1;
            }
            return;
        }

        public friendComboxChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            this._addresseeText.text = this._friendComboBox.selectedItem.label;
            return;
        }

        public friendComboOpenHandler(arg1: Object /* flash.events.Event */): void
        {
            var loc3=0;
            var loc4=null;
            var loc1=new DataProvider();
            var loc2=Cache.instance.friend.getFriendList();
            if (loc2) 
            {
                while (loc3 < loc2.length) 
                {
                    loc4 = loc2[loc3] as FriendInfo;
                    loc1.addItem({"label": loc4.friendPlayerInfo.playerName});
                    ++loc3;
                }
                this._friendComboBox.dataProvider = loc1;
            }
            return;
        }

        public onSendBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._smail.toPlayerName = this._addresseeText.text;
            this._smail.title = this._titleText.text;
            this._smail.content = this._contentTextArea.text;
            this._smail.attachmentCoin = int(this._coinText.text);
            this._smail.attachmentGold = int(this._goldText.text);
            if (Cache.instance.role.money.coinBind + Cache.instance.role.money.coin < this.getPostate()) 
            {
                Alert.show(Language.getString(52028));
                return;
            }
            if (this.isHasAtt() && StringHelper.getCharLength(StringHelper.trim(this._smail.title)) == 0 && StringHelper.getCharLength(StringHelper.trim(this._smail.content)) == 0) 
            {
                if (MailUtil.isDictHasItem(this._uidDict)) 
                {
                    this._smail.title = Language.getString(52055);
                }
                else if (this._inputMoneyPanel.coin > 0) 
                {
                    this._smail.title = Language.getString(52056);
                }
                else if (this._inputMoneyPanel.gold > 0) 
                {
                    this._smail.title = Language.getString(52057);
                }
                this._smail.content = Language.getString(52058);
            }
            Dispatcher.dispatchEvent(new DataEvent(MailEventName.SEND_CLICK, {"smail": _smail, "uids": _uidDict}));
            return;
        }

        /* internal  */isHasAtt(): Boolean
        {
            return MailUtil.isDictHasItem(this._uidDict) || this._inputMoneyPanel && (this._inputMoneyPanel.coin > 0 || this._inputMoneyPanel.gold > 0);
        }

        public onInputMoneyBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._inputMoneyPanel) 
            {
                this._inputMoneyPanel.update(null);
            }
            else 
            {
                this._inputMoneyPanel = new InputMoneyPanel();
            }
            this._inputMoneyPanel.show();
            return;
        }

        /* internal  */moveInHandler(arg1: DragEvent): void
        {
            var loc3=null;
            var loc4=null;
            var loc1=arg1.dragItem as BaseItem;
            var loc2=arg1.dropItem as AttachmentItem;
            this._currentAcItem = loc2;
            if (loc1 && loc2) 
            {
                loc3 = loc1.itemData;
                if (loc4 = loc2.itemData) 
                {
                    delete this._uidDict[loc4.uid];
                    Cache.instance.mail.useItem[loc4.uid] = 0;
                }
                loc2.dragSource = loc3;
                if (loc3) 
                {
                    if (loc3.itemAmount > 1) 
                    {
                        if (!this._inputAmountPanel) 
                        {
                            this._inputAmountPanel = new InputAmountPanel();
                        }
                        this._inputAmountPanel.itemData = loc3;
                    }
                    else 
                    {
                        this._amount = 1;
                    }
                    this._uidDict[loc3.uid] = this._amount;
                    Cache.instance.mail.useItem[loc3.uid] = loc3.itemAmount;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Mail_StatusChange, {"itemData": loc3, "amount": loc3.itemAmount}));
                }
            }
            this.updatePostage();
            return;
        }

        /* internal  */throwHandler(arg1: DragEvent): void
        {
            var loc1=arg1.dragItem as AttachmentItem;
            var loc2=arg1.dropItem as BaseItem;
            this.removeAttachment(loc1);
            return;
        }

        public getPostate(): int
        {
            var loc1=this._postage;
            return loc1;
        }

        public updatePostage(): void
        {
            this._postageText.text = Language.getString(52025) + this.getPostate();
            return;
        }

        public onInputMoneyOKClickHandler(arg1: DataEvent): void
        {
            var loc1=Number(arg1.data["coin"]);
            var loc2=int(arg1.data["gold"]);
            if (loc1 > Cache.instance.role.money.coin) 
            {
                Alert.show(Language.getString(52029));
                return;
            }
            if (loc2 > Cache.instance.role.money.gold) 
            {
                Alert.show(Language.getString(52030));
                return;
            }
            this._coinText.text = arg1.data["coin"];
            this._goldText.text = arg1.data["gold"];
            this.updatePostage();
            return;
        }

        public onAttachmentClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.target as AttachmentItem;
            this.removeAttachment(loc1);
            return;
        }

        public removeAttachment(arg1: AttachmentItem): void
        {
            var loc1=null;
            if (arg1) 
            {
                if (arg1.dragSource) 
                {
                    loc1 = arg1.dragSource as ItemData;
                    delete this._uidDict[loc1.uid];
                    Cache.instance.mail.useItem[loc1.uid] = 0;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Mail_StatusChange, {"itemData": loc1, "amount": 0}));
                }
                arg1.detory();
            }
            this.updatePostage();
            return;
        }

        public clearAttachment(): void
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < this._itemsArray.length) 
            {
                if (this._itemsArray[loc2]) 
                {
                    loc1 = this._itemsArray[loc2] as AttachmentItem;
                    loc1.detory();
                }
                ++loc2;
            }
            return;
        }

        public clearPanel(): void
        {
            this._titleText.text = "";
            this._contentTextArea.text = "";
            this._goldText.text = "0";
            this._coinText.text = "0";
            this._uidDict = new Map<any, any> /* flash.utils.Dictionary */();
            this.clearAttachment();
            this._postageText.text = Language.getString(52025) + this._postage;
            return;
        }

        public /* override */ set visible(arg1: Boolean)
        {
            super.visible = arg1;
            this.dispatchEvent(new Object /* flash.events.Event */(MailEventName.WRITE_CHANGE, true));
            return;
        }

        /* internal */ /* var */_amount: int;

        /* internal */ /* var */_titleText: GTextInput;

        /* internal */ /* var */_currentAcItem: AttachmentItem;

        /* internal */ /* var */_addresseeLabel: GLabel;

        /* internal */ /* var */_addresseeText: GTextInput;

        /* internal */ /* var */_friendComboBox: GComboBox;

        /* internal */ /* var */_uidDict: Map<any, any> /* flash.utils.Dictionary */;

        /* internal */ /* var */_titleLabel: GLabel;

        /* internal */ /* var */_contentTextArea: MTextArea;

        /* internal */ /* var */_inputMoneyPanel: InputMoneyPanel;

        /* internal */ /* var */_inputAmountPanel: InputAmountPanel;

        /* internal */ /* var */_goodsLabel: GLabel;

        /* internal */ /* var */_goldBitmap: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_coinBitmap: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_goldText: GTextInput;

        /* internal */ /* var */_coinText: GTextInput;

        /* internal */ /* var */_postageText: GTextInput;

        /* internal */ /* var */_tipsLabel: GLabel;

        /* internal */ /* var */_inputMoneyBtn: GButton;

        /* internal */ /* var */_sendBtn: GButton;

        /* internal */ /* var */_smail: SMail;

        /* internal */ /* var */_itemsArray: Array<any>;

        /* internal */ /* var */_postage: int;
    }
